package py.com.fms.fms.persistence.services;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.fms.bean.GenParamBean;
import py.com.fms.com.bean.SupplyCostStockBean;
import py.com.fms.exception.CommonException;
import py.com.fms.persistence.services.FmsServices;

public class GenParamServices extends FmsServices{


	public GenParamServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	@SuppressWarnings("unchecked")
	public List<GenParamBean> getAllGenParam() throws CommonException{
		List<GenParamBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the monedas ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsMonedas.xml   ]]
			
			queryForList = (List<GenParamBean>)this.getConfig().queryForList("genparam.getAllGenParam");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 [[[
	 [[ The following method store a bean (record) in the database.
	 */
	
	public void saveNewGenParam(GenParamBean pGenParam) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("genparam.insGenParam", pGenParam);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method delete a record from the database.
	 */
	
	public void deleteGenParamById(long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idGenParam", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("genparam.delGenParamById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method update a record from a bean.
	 */
	public void updateGenParam(GenParamBean pGenParam) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("genparam.updGenParamById", pGenParam);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method retrieve a object containing the record id
	 */

	public long getIdGenParam(long pIdEnt,  String pCod) throws CommonException{
		long queryForObject;
		try {
			
			//[[ queryForList is an implemented interface that receive as argument the cuentas bancarias ]] 
			//[[ namespace located in package py.com.fms.persistence.sqlmap.sql.fms.fmsParamGrales.xml   ]]
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idEnt", pIdEnt);
			param.put("cod", pCod);
			queryForObject = (Long)this.getConfig().queryForObject("genparam.getIdGenParam", param);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}

	/**
	 * Generado para devolver el bean a partir de la empresa y el codigo de parametro.
	 */
	public GenParamBean getGenParamBeanByEntAndCod(Long pIdEnt,  String pCod) throws CommonException{
		GenParamBean queryForObject;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idEnt", pIdEnt);
			param.put("cod", pCod);
			queryForObject = (GenParamBean)this.getConfig().queryForObject("genparam.getGenParamBeanByEntCod", param);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}

}
